package krow.dev.contest;

import java.util.*;

import krow.dev.contest.Constants.Invoke;
import krow.dev.contest.controller.*;
import krow.dev.contest.model.MenuItem;
import krow.dev.contest.view.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {
	
	private MainMenuListAdapter mMenuListAdapter;
	
	private ListView mMenuListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		

		initializeView();
		
		buildMenu();
	
	}
	
	private void initializeView(){
		
		mMenuListAdapter = new MainMenuListAdapter(getApplicationContext());
		mMenuListView = (ListView) findViewById(R.id.list_menu);
		mMenuListView.addHeaderView(getLayoutInflater().inflate(R.layout.header_menu, null));
		mMenuListView.setOnItemClickListener(mMenuItemClickListener);
		mMenuListView.setAdapter(mMenuListAdapter);
	}

	// 메뉴 구성하는데 시간이 지연된다면, 추후 비동기로 처리
		private void buildMenu(){
			
			MenuBuilder menuBuilder = new MenuBuilder(getApplicationContext());
			menuBuilder.addMenu(
					0, 
					getString(R.string.menu_department), "department");
			menuBuilder.addMenu(
					0, 
					getString(R.string.menu_target), "target");
			menuBuilder.addMenu(
					0,
					getString(R.string.menu_enterprise), "enterprise");
			menuBuilder.addMenu(
					0,
					getString(R.string.menu_prize), "prize");
			menuBuilder.addMenu(
					0,
					getString(R.string.menu_benefits), "benefits");
			
		
			//TODO push, 전화 걸기, 데이터 수집 등 10개 안팍으로 메뉴 구성 예정
			
			List<MenuItem> menuItemList = menuBuilder.build();
			
			mMenuListAdapter.setMenuItemList(menuItemList);
			mMenuListAdapter.notifyDataSetChanged();
		}
		
		private AdapterView.OnItemClickListener mMenuItemClickListener = new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if(view.getTag() == null){
					return ;
				}
				
				MenuItem menuItem = (MenuItem) view.getTag();

				Intent intent = new Intent(MainActivity.this, SubActivity.class);
				intent.putExtra(Invoke.KEY_RssAddr, menuItem.getRssAddr());
				startActivity(intent);
			}
		};

}
